package com.beerbook;

import java.io.File;
import java.io.FilenameFilter;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class BeerDetails extends ListActivity implements OnClickListener {

	protected TextView nameText;
	protected TextView breweryText;
	protected TextView categoryText;
	protected TextView styleText;
	protected TextView abvText;
	protected TextView ibuText;
	protected TextView dateText;
	protected TextView ratingText;
	protected TextView notesText;
	protected TextView pictureText;
    protected int beerId;
    private Uri[] mUrls;
    String[] mFiles =  null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beer_details);
		Button delete_button = (Button)findViewById(R.id.Delete);
		Button edit_button = (Button)findViewById(R.id.Edit);
		delete_button.setOnClickListener(this);
		edit_button.setOnClickListener(this);
		
        showBeer();
    }
    
    public void showBeer(){
        beerId = getIntent().getIntExtra("BEER_ID", 0);
        SQLiteDatabase db = (new BeerDB(this)).getWritableDatabase();
        final Beer beer = new Beer(db,beerId);
        db.close();
        
        //Load Image
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
	        File images = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+"/beerbook");
	        File[] imagelist = images.listFiles(new FilenameFilter(){
	        	public boolean accept(File dir, String name){
	        		if(beer.getPicture()!=null){
	        			return ((name.contains(beer.getPicture())));
	        		}
	        		return false;
	        	}
	        });
	        if(imagelist != null){
		        mFiles = new String[imagelist.length];
		        
		        for(int i = 0; i<imagelist.length;i++){
		        	mFiles[i]=imagelist[i].getAbsolutePath();
		        }
		        mUrls = new Uri[mFiles.length];
		        
		        for(int i=0;i<mFiles.length;i++){
		        	mUrls[i]=Uri.parse(mFiles[i]);
		        }
		        
		        Gallery g = (Gallery)findViewById(R.id.beerGallery);
		        g.setAdapter(new ImageAdapter(this));
		        g.setFadingEdgeLength(40);
	        }
        }
        
    	//Set Values
        nameText = (TextView) findViewById(R.id.name);
        nameText.setText(beer.getName());

        breweryText = (TextView) findViewById(R.id.brewery);
        breweryText.setText(beer.getBrewery());
        
        categoryText = (TextView) findViewById(R.id.category);
        categoryText.setText(beer.getCategory());
        
        styleText = (TextView) findViewById(R.id.style);
        styleText.setText(beer.getStyle());
        
        abvText = (TextView) findViewById(R.id.abv);
        abvText.setText(String.valueOf(beer.getAbv()));
        
        ibuText = (TextView) findViewById(R.id.ibu);
        ibuText.setText(String.valueOf(beer.getIbu()));
        
        dateText = (TextView) findViewById(R.id.date);
        dateText.setText(beer.getDate());
        
        ratingText = (TextView) findViewById(R.id.rating);
        ratingText.setText(String.valueOf(beer.getRating()));
        
        notesText = (TextView) findViewById(R.id.notes);
        notesText.setText(beer.getNotes());
    }

	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.Edit:
				Intent i = new Intent(this, EditBeer.class); 
				i.putExtra("beerId", beerId); 
				startActivity(i);
				break;
			case R.id.Delete:
				beerId = getIntent().getIntExtra("BEER_ID", 0);
			    SQLiteDatabase db = (new BeerDB(this)).getWritableDatabase();
			    Beer b = new Beer(db,beerId);
				deletebeer(b);
				finish();
				db.close();
				break;
		}    
    }

	private void deletebeer(Beer b) {
		SQLiteDatabase db = (new BeerDB(this)).getWritableDatabase();
		db.delete("beer", "_id" + "=?", new String[]{Integer.toString(b.getId())});
		db.close();
	}
	
	public void onResume(){
 		super.onResume();
 		showBeer();
 	}

    public class ImageAdapter extends BaseAdapter {
    	int mGalleryItemBackground;
    	public ImageAdapter(Context c){
    		mContext = c;
    	}
    	public int getCount(){
    		return mUrls.length;
    	}
    	public Object getItem(int position){
    		return position;
    	}
    	public long getItemId(int position){
    		return position;
    	}
    	public View getView(int position, View convertView, ViewGroup partent){
    		ImageView i = new ImageView(mContext);
    		
    		i.setImageURI(mUrls[position]);
    		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    		i.setLayoutParams(new Gallery.LayoutParams(260,210));
    		return i;
    	}
    	private Context mContext;
    }

}